package com.myapp.util.security;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

class InnerCipher implements AppCipher
{
	private static AlgorithmParameterSpec paramSpec = null;
	private static Key key = null;
	private String cipherKey = null;
	private Cipher cipher = null;
	
	public static final String DES_KEY = "DES";
	public static final String DEFAULT_KEY = "etra_key";
	public static final String DES_CBC_TYPE = "DES/CBC/PKCS5Padding";
	public static final String UTF8_CHARSET_TYPE = "utf-8";
	
	public void init(String appCipherKey) throws Exception
	{		
		if(!StringUtils.isBlank(appCipherKey))
		{
			if(appCipherKey.length() < DESKeySpec.DES_KEY_LEN)
			{
				appCipherKey = appCipherKey.concat(DEFAULT_KEY).substring(0, DESKeySpec.DES_KEY_LEN);
			}else if(appCipherKey.length() > 8){
				appCipherKey = appCipherKey.substring(0, DESKeySpec.DES_KEY_LEN);
			}
			if(!appCipherKey.equals(cipherKey)){
				cipherKey = appCipherKey;
			}else{
				return;
			}
		}else
		{
			if(!cipherKey.equals(DES_KEY))
			{
				cipherKey = DES_KEY;					
			}else
			{
				return;
			}				
		}
			
		if(cipher == null)
		{
			cipher  =  Cipher.getInstance(DES_CBC_TYPE);
		}
		byte [] cipherKeyBytes = cipherKey.getBytes();
		DESKeySpec keySpec = new DESKeySpec(cipherKeyBytes);
		paramSpec = new IvParameterSpec(cipherKeyBytes);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_KEY);
		key = keyFactory.generateSecret(keySpec);
	}
	
	@Override
	public String encrypt(String data, String encryptionKey) throws Exception {
		init(encryptionKey);
		cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec);
		return Base64.encodeBase64URLSafeString(cipher.doFinal(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(data)));
	}
	
	@Override
	public String decrypt(String data, String encryptionKey) throws Exception {
		init(encryptionKey);
		cipher.init(Cipher.DECRYPT_MODE,key,paramSpec);
		return org.apache.commons.codec.binary.StringUtils.newStringUtf8(cipher.doFinal(Base64.decodeBase64(data)));
	}	 
}
public class CryptoUtils{
	
	private static AppCipher cipherInstance = null;
	
	public static final String CRYPTO_DEFAULT_KEY = InnerCipher.DEFAULT_KEY;
	
	public static AppCipher getInstance(){
		if(cipherInstance == null){
			cipherInstance = new InnerCipher();
		}
		return cipherInstance;		
	}
	
	public static void main(String []args)
	{
		try {			
			System.out.println(CryptoUtils.getInstance().encrypt("11111111", "etra_passwd_key"));
			System.out.println(CryptoUtils.getInstance().decrypt("OpAGqJVNPHC2S5Vr8nwjvyyDZHlOLaorQMocdR_6Fmk", CryptoUtils.CRYPTO_DEFAULT_KEY));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


